package com.whfc.wxmp.api.base.controller;

import com.whfc.base.dto.MsgRecordDTO;
import com.whfc.base.dto.MsgStatisticsDTO;
import com.whfc.base.param.WarnMsgSearchParam;
import com.whfc.common.enums.AppWarnMsgChannel;
import com.whfc.common.result.*;
import com.whfc.common.util.AssertUtil;
import com.whfc.common.util.JSONUtil;
import com.whfc.wxmp.annotation.CurrentWxUser;
import com.whfc.fuum.entity.WxUser;
import com.whfc.entity.dto.msg.AppWarnMsgDTO;
import com.whfc.entity.param.msg.AppMsgReadParam;
import com.whfc.wxmp.api.base.service.WxMessageService;
import com.whfc.wxmp.config.AbstractBaseController;
import com.whfc.wxmp.util.RequestConetxtUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.List;


/**
 * @Description: 我的消息接口
 * @author: xugcheng
 * @version: 1.0
 * @date: 2019/11/30 8:49
 */
@RestController
@RequestMapping("mp/api/msg")
public class WxMessageController extends AbstractBaseController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WxMessageService wxMessageService;

    /**
     * 查询报警消息概况
     *
     * @param loginUser
     * @param deptId
     * @return
     */
    @GetMapping("warn/general")
    public Result getWarnGeneral(@CurrentWxUser WxUser loginUser,
                                 @RequestParam("deptId") Integer deptId) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                loginUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "查询报警消息概况", RequestConetxtUtil.getParameters());
        ListData<AppWarnMsgDTO> data = wxMessageService.getWarnGeneral(deptId, loginUser.getPhone());
        return ResultUtil.success(data);
    }

    /**
     * 查询报警消息列表
     *
     * @param loginUser
     * @param deptId
     * @param handleState
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("warn/list")
    public Result getWarnMsgList(@CurrentWxUser WxUser loginUser,
                                 @RequestParam("deptId") Integer deptId,
                                 @RequestParam(value = "handleState", required = false) Integer handleState,
                                 @RequestParam(value = "moduleType", required = false) Integer moduleType,
                                 @RequestParam("pageNum") Integer pageNum,
                                 @RequestParam("pageSize") Integer pageSize) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                loginUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "查询报警消息列表", RequestConetxtUtil.getParameters());
        PageData<AppWarnMsgDTO> data = wxMessageService.getWarnMsgList(deptId, loginUser.getPhone(), handleState, moduleType, pageNum, pageSize);
        return ResultUtil.success(data);
    }

    /**
     * 设置消息已读
     *
     * @param loginUser
     * @param param
     * @return
     */
    @PostMapping("read")
    public Result readMsg(@CurrentWxUser WxUser loginUser,
                          @RequestBody @Validated AppMsgReadParam param) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                loginUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "设置消息已读", JSONUtil.toString(param));
        wxMessageService.setMsgRead(param.getIdList());
        return ResultUtil.success();
    }

    @PostMapping("del")
    public Result delMsg(@CurrentWxUser WxUser loginUser,
                         @RequestBody @Validated AppMsgReadParam param) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                loginUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "删除消息", JSONUtil.toString(param));
        wxMessageService.delMsg(param.getIdList());
        return ResultUtil.success();
    }

    /**
     * 2.0 首页未处理消息
     * @param user
     * @param pageNum
     * @param pageSize
     * @param deptId
     * @return
     */
    @GetMapping("/list/home")
    public Result listHome(@CurrentWxUser WxUser user,
                       @RequestParam("pageNum") Integer pageNum,
                       @RequestParam("pageSize") Integer pageSize,
                       @RequestParam("deptId") Integer deptId
    ) {
        logger.info("首页消息列表，loginer:{},pageNum:{},PageSize:{},deptId:{},state:{},moduleType:{}",
                user.getName(), pageNum, pageSize, deptId, 0, null);
        PageData<AppWarnMsgDTO> pageData = wxMessageService.list(user.getPhone(), pageNum, pageSize, deptId, 0, null);
        return ResultUtil.success(pageData);
    }

    /**
     * 2.0.0报警消息类型数据统计
     *
     * @param deptId   项目ID
     * @param warnType 报警类型 1-报警消息 0-提醒消息
     * @return
     */
    @GetMapping("/warn/statistics/list")
    public R<List<MsgStatisticsDTO>> getWarnMsgStatistics(@RequestParam Integer deptId, @RequestParam Integer warnType) {
        Integer userId = getUserId();
        logger.info("报警消息列表，userId:{},deptId:{},warnType:{},deptId:{},state:{},warnType:{}", userId, deptId, warnType);
        if (warnType < 0 || warnType > 1 || userId == null) {
            return R.success(Collections.emptyList());
        }
        List<MsgStatisticsDTO> res = wxMessageService.getWarnMsgStatistics(deptId, warnType, userId);
        return R.success(res);
    }

    /**
     * 2.0.0报警消息分页
     *
     * @param param
     * @return
     */
    @GetMapping("/warn/page")
    public R<PageVO<MsgRecordDTO>> getWarnMsgPage(@Validated WarnMsgSearchParam param) {
        param.setUserId(getUserId());
        param.setMsgChannel(AppWarnMsgChannel.WXMP.value());
        logger.info("报警消息明细分页:{}", param);
        AssertUtil.isTrue(param.getDeptId() == null || param.getModuleType() == null, "参数缺失");
        PageVO<MsgRecordDTO> res = wxMessageService.getWarnMsgPage(param);
        return R.success(res);
    }

}
